{extend name="public/base" /}
{block name='body'}
<div class="layui-container fly-marginTop">
    <div class="fly-panel" pad20 style="padding-top: 5px;">
        <!--<div class="fly-none">没有权限</div>-->
        <div class="layui-form layui-form-pane">
            <div class="layui-tab layui-tab-brief" lay-filter="user">
                <ul class="layui-tab-title">
                    <li class="layui-this">分享资源<!-- 编辑帖子 --></li>
                </ul>
                <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0;">
                    <div class="layui-tab-item layui-show">
                        <div class="layui-form">
                            <div class="layui-row layui-col-space15 layui-form-item">
                                <div class="layui-col-md3">
                                    <label class="layui-form-label">所在分类</label>
                                    <div class="layui-input-block">
                                        <select lay-verify="required" name="FK_ResourceCategoryGuid"
                                                lay-filter="column">
                                            <option></option>
                                            {volist name='cats' id='cat'}
                                            <option value="{$cat.PK_ResourceCategoryGuid}">{$cat.Name}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-col-md9">
                                    <label for="L_title" class="layui-form-label">标题</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="L_title" name="Title" required lay-verify="required"
                                               autocomplete="off" class="layui-input">
                                        <!-- <input type="hidden" name="id" value="{{d.edit.id}}"> -->
                                    </div>
                                </div>
                            </div>
                            <div class="layui-row layui-col-space15 layui-form-item">
                                <div class="layui-col-md4">
                                    <label class="layui-form-label">资源类型</label>
                                    <div class="layui-input-block">
                                        <select name="restype" lay-filter="restype">
                                            <option></option>
                                            {volist name='rt' id='t'}
                                            <option value="{$t}">{$t}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>

                                <!--网盘分享开始-->
                                <div class="layui-col-md6 layui-hide pan" value="网盘资源">
                                    <label class="layui-form-label" for="L_version">网盘地址</label>
                                    <div class="layui-input-block">
                                        <input type="text" value="" name="panurl" autocomplete="off"
                                               class="layui-input" placeholder="网盘地址,带http或https的地址">
                                    </div>
                                </div>
                                <div class="layui-col-md2 layui-hide pan" value="网盘资源">
                                    <label class="layui-form-label" for="L_browser">分享密码</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="L_browser" value="" name="panpwd"
                                               placeholder="密码" autocomplete="off"
                                               class="layui-input">
                                    </div>
                                </div>
                                <!--网盘分享结束-->

                                <!--实体资源-->
                                <div class="layui-col-md8 layui-hide pan" value="实体资源">
                                    <button type="button" class="layui-btn" id="upRes"><i class="layui-icon"></i>上传文件
                                    </button>
                                    <span style="color: red;padding: 2px 10px;"
                                          id="fileInfo">文件10M以内,过大的资源请上传百度网盘</span>
                                </div>

                                <!--URL资源-->
                                <div class="layui-col-md8 layui-hide pan" value="URL资源">
                                    <label class="layui-form-label" for="L_version">URL地址</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="L" value="" name="weburl" autocomplete="off"
                                               class="layui-input" placeholder="请输入完整的url地址">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item layui-form-text">
                                <div class="layui-input-block">
                                    <textarea name="Desc" required lay-verify="required" placeholder="资源详细描述" class="layui-textarea fly-editor"  style="height: 260px;"></textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">所需金币</label>
                                    <div class="layui-input-inline" style="width: 190px;">
                                        <select name="Price">
                                            <option></option>
                                            {volist name='pri' id='p'}
                                            <option value="{$p}">{$p}金币</option>
                                            {/volist}
                                        </select>
                                    </div>
                                    <div class="layui-form-mid layui-word-aux">分享后无法修改金币</div>
                                </div>

                                <!--验证码-->
                                <div class="layui-form-item">
                                    <label for="L_vercode" class="layui-form-label">验证码</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="L_vercode" name="vercode" required lay-verify="required"
                                               placeholder="填写验证码" autocomplete="off" class="layui-input">
                                    </div>
                                    <div class="layui-form-mid">
                                        <span style="color: #c00;"><div class="sw-verify-img"><img
                                                src="{:url('base/vercode','id=share')}" alt=""
                                                onclick="this.src=this.src+'?'+Math.random()"></div></span>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <button class="layui-btn" id="shareRes" lay-filter="share" lay-submit>立即发布</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}
{block name='script'}
<script>
    layui.config(
        {
            base: "__JS__/"
        }
    ).use(['form', 'jquery', 'layer', 'upload', 'res'], function () {
        let form = layui.form;
        let layer = layui.layer;
        let $ = layui.jquery;
        let upload = layui.upload, resopnse = layui.res;
        let fileUrl = '';
        let fileName = '';
        let isFile = false;

        /**
         * 资源类别选择
         */
        form.on('select(restype)', function (data) {
            let val = data.value;//当前选中的资源类型的值
            $(".pan").addClass('layui-hide');
            $("div[value='" + val + "']").removeClass('layui-hide');

        });


        /**
         * 提交资源
         */
        form.on('submit(share)', function (data) {
            let resData = data.field;
            let vercode = resData.vercode;
            if (resData.restype === '实体资源') {
                if (!fileUrl) return layer.alert('请上传你的附件');
            }
            $.post("{:url('base/checkVercode')}", {"code": vercode, "id": "share"}, function (res) {
                if (res.errno === 0) {
                    //验证码正确
                    /*
                    Title:sdfds
                    restype:网盘资源
                    panurl:sdf
                    panpwd:sdf
                    weburl:
                    Desc:sdsdf
                    Price:1
                    vercode:hv4x
                     */

                    resData.type=resData.restype;
                    switch (resData.restype) {
                        case "网盘资源": {
                            resData.type_value = resData.panurl + "-" + resData.panpwd;
                        }
                            break;
                        case "URL资源": {
                            resData.type_value = resData.weburl;
                        }
                            break;
                        case "实体资源": {
                            resData.type_value = fileUrl;
                        }
                    }
                    $.post("{:url('user/share')}", resData, function (res) {
                        resopnse.add(res);
                    })

                } else {
                    //验证码错误
                    return layer.msg('验证码错误');
                }
            });

        })

        /**
         * 文件上传
         */
        upload.render({
            elem: '#upRes'
            , url: "{:url('base/upload')}"
            , size: 10240
            , drag: true
            , accept: 'file'
            , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                layer.load('生产者'); //上传loading
                obj.preview(function () {
                    fileName = arguments[1].name;
                });
            }
            , done: function (res, index, upload) {
                if (res.errno === 0) {
                    fileUrl = res.data;
                }
                layer.closeAll('loading'); //关闭loading
                layer.alert("文件[" + fileName + "] 上传成功");
                $("#fileInfo").html("文件[" + fileName + "]上传成功");
                isFile = true;
            }
            , error: function (index, upload) {
                layer.closeAll('loading'); //关闭loading
                layer.msg('系统繁忙,请稍后再试');
            }
        });


    });

</script>
{/block}